import smtplib
from email.mime.text import MIMEText
from email.header import Header, decode_header
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

# 配置邮件
sendEmail = False
qqEmail = ''
qqPassword = ''


def sendEmailWithFile(content, title='无主题', imgp='', fp='', to='1046999582@qq.com'):
    if sendEmail:
        # 创建 SMTP 对象
        smtp = smtplib.SMTP()
        # 连接（connect）指定服务器
        smtp.connect("smtp.qq.com", port=25)
        # 登录，需要：登录邮箱和授权码
        smtp.login(user=qqEmail, password=qqPassword)
        # 构造MIMEText对象，参数为：正文，MIME的subtype，编码方式
        message = MIMEMultipart('mixed')
        text_plain = MIMEText(content, 'plain', 'utf-8')
        message.attach(text_plain)
        message['From'] = "3094830052@qq.com"  # 发件人的昵称
        message['To'] = "zk"  # 收件人的昵称
        message['Subject'] = Header(title, 'utf-8')  # 定义主题内容
        if imgp != '':
            # 构造图片附件
            send_image_file = open(imgp, 'rb').read()  # 打开文件，可以使用相对路劲和绝对路径
            image = MIMEImage(send_image_file)
            image.add_header('Content-ID', '<image1>')
            image["Content-Disposition"] = 'attachment; filename="1.jpg"'
            message.attach(image)
        if fp != '':
            # 构造附件
            send_file = open(fp, 'rb').read()
            text_att = MIMEText(send_file, 'base64', 'utf-8')
            text_att["Content-Type"] = 'application/octet-stream'
            # 重命名附件文件
            text_att.add_header('Content-Disposition', 'attachment', filename='1.txt')
            message.attach(text_att)

            # 发送邮件
        smtp.sendmail(from_addr="3094830052@qq.com", to_addrs=to, msg=message.as_string("utf-8"))
